<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Decorator  Reference</title>
    <link rel="stylesheet" type="text/css" href="css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="css/highlight.css" />
    <meta charset='utf-8'>
    <script src="js/jquery.min.js" defer></script>
    <script src="js/jazzy.js" defer></script>
    
    <script src="js/lunr.min.js" defer></script>
    <script src="js/typeahead.jquery.js" defer></script>
    <script src="js/jazzy.search.js" defer></script>
  </head>
  <body>
    <a title="Decorator  Reference"></a>
    <header>
      <div class="content-wrapper">
        <p><a href="index.html">Sourcery 2.3.0 Docs</a> (100% documented)</p>
        <p class="header-right"><a href="https://github.com/krzysztofzablocki/Sourcery"><img src="img/gh.png"/>View on GitHub</a></p>
        <p class="header-right">
          <form role="search" action="search.json">
            <input type="text" placeholder="Search documentation" data-typeahead>
          </form>
        </p>
      </div>
    </header>
    <div class="content-wrapper">
      <p id="breadcrumbs">
        <a href="index.html">Sourcery Reference</a>
        <img id="carat" src="img/carat.png" />
        Decorator  Reference
      </p>
    </div>
    <div class="content-wrapper">
      <nav class="sidebar">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a href="Guides.html">Guides</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="installing.html">Installing</a>
              </li>
              <li class="nav-group-task">
                <a href="usage.html">Usage</a>
              </li>
              <li class="nav-group-task">
                <a href="writing-templates.html">Writing templates</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Examples.html">Examples</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="equatable.html">Equatable</a>
              </li>
              <li class="nav-group-task">
                <a href="hashable.html">Hashable</a>
              </li>
              <li class="nav-group-task">
                <a href="enum-cases.html">Enum cases</a>
              </li>
              <li class="nav-group-task">
                <a href="lenses.html">Lenses</a>
              </li>
              <li class="nav-group-task">
                <a href="mocks.html">Mocks</a>
              </li>
              <li class="nav-group-task">
                <a href="codable.html">Codable</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/Diffable.html">Diffable</a>
              </li>
              <li class="nav-group-task">
                <a href="diffable.html">Diffable</a>
              </li>
              <li class="nav-group-task">
                <a href="linuxmain.html">LinuxMain</a>
              </li>
              <li class="nav-group-task">
                <a href="decorator.html">Decorator</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Types.html">Types</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Classes/Types.html">Types</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Type.html">Type</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Protocol.html">Protocol</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Class.html">Class</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Struct.html">Struct</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Enum.html">Enum</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/EnumCase.html">EnumCase</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/AssociatedValue.html">AssociatedValue</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/AssociatedType.html">AssociatedType</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Variable.html">Variable</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Method.html">Method</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/MethodParameter.html">MethodParameter</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Subscript.html">Subscript</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/TypeName.html">TypeName</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/TupleType.html">TupleType</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/TupleElement.html">TupleElement</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/ArrayType.html">ArrayType</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/DictionaryType.html">DictionaryType</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/ClosureType.html">ClosureType</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/GenericType.html">GenericType</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/GenericTypeParameter.html">GenericTypeParameter</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Attribute.html">Attribute</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/ProtocolComposition.html">ProtocolComposition</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Classes.html">Other Classes</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Classes/Actor.html">Actor</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/ClosureParameter.html">ClosureParameter</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/DiffableResult.html">DiffableResult</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/GenericParameter.html">GenericParameter</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/GenericRequirement.html">GenericRequirement</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/GenericRequirement/Relationship.html">– Relationship</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Import.html">Import</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Modifier.html">Modifier</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/SetType.html">SetType</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Enums.html">Other Enumerations</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Enums/Composer.html">Composer</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Extensions.html">Other Extensions</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Extensions/Array.html">Array</a>
              </li>
              <li class="nav-group-task">
                <a href="Other%20Extensions.html#/c:@M@SourceryRuntime@objc(cs)BytesRange">BytesRange</a>
              </li>
              <li class="nav-group-task">
                <a href="Other%20Extensions.html#/c:@M@SourceryRuntime@objc(cs)FileParserResult">FileParserResult</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/String.html">String</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/StringProtocol.html">StringProtocol</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/Typealias.html">Typealias</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Protocols.html">Other Protocols</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Protocols/Annotated.html">Annotated</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/Definition.html">Definition</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/Documented.html">Documented</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/Typed.html">Typed</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Typealiases.html">Other Type Aliases</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Other%20Typealiases.html#/s:15SourceryRuntime11Annotationsa">Annotations</a>
              </li>
              <li class="nav-group-task">
                <a href="Other%20Typealiases.html#/s:15SourceryRuntime13Documentationa">Documentation</a>
              </li>
              <li class="nav-group-task">
                <a href="Other%20Typealiases.html#/s:15SourceryRuntime0A8Modifiera">SourceryModifier</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">
        <section>
          <section class="section">
            
            <h2 id='i-want-to-generate-simple-decorator-for-my-type' class='heading'>I want to generate simple decorator for my type</h2>

<p>In Swift it can be cumbersome to write a simple decorator that decorates all the calls to decorated type methods, you basically have to do it manually for each single method. With this template you can generate simple decorator that generate all the methods and property calls automatically, skipping methods and properties already implemented manually.</p>

<p>You can use this template as a starting point for more sophisticated implementations. This template also shows you some powers of swift templates, like using helper methods and whitespace control tags.</p>
<h3 id='a-href-https-github-com-krzysztofzablocki-sourcery-blob-master-templates-templates-decorator-swifttemplate-swift-template-a' class='heading'><a href="https://github.com/krzysztofzablocki/Sourcery/blob/master/Templates/Templates/Decorator.swifttemplate">Swift template</a></h3>
<h4 id='available-annotations' class='heading'>Available annotations</h4>

<ul>
<li><code>decorate</code> - what type to decorate</li>
<li><code>decorateMethod</code> - code to decorate each method call with</li>
<li><code>decorateGet</code> - code to decorate each property getter</li>
<li><code>decorateSet</code> - code to decorate each property setter</li>
</ul>

<p><strong>Example input:</strong></p>
<pre class="highlight swift"><code><span class="kd">protocol</span> <span class="kt">Service</span> <span class="p">{</span>
    <span class="k">var</span> <span class="nv">prop1</span><span class="p">:</span> <span class="kt">Int</span> <span class="p">{</span> <span class="k">get</span> <span class="p">}</span>
    <span class="k">var</span> <span class="nv">prop2</span><span class="p">:</span> <span class="kt">Int</span> <span class="p">{</span> <span class="k">get</span> <span class="k">set</span> <span class="p">}</span>
    <span class="kd">func</span> <span class="nf">foo</span><span class="p">(</span><span class="nv">f</span><span class="p">:</span> <span class="kt">Int</span><span class="p">,</span> <span class="n">_</span> <span class="nv">a</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="k">throws</span> <span class="o">-&gt;</span> <span class="kt">Int</span>
    <span class="kd">func</span> <span class="nf">bar</span><span class="p">(</span><span class="n">_</span> <span class="nv">b</span><span class="p">:</span> <span class="kt">String</span><span class="p">)</span>
<span class="p">}</span>

<span class="c1">// sourcery: decorate = "Service"</span>
<span class="c1">// sourcery: decorateMethod = "print(#function)"</span>
<span class="c1">// sourcery: decorateGet = "print("get: \(#function)")"</span>
<span class="c1">// sourcery: decorateSet = "print("set: \(#function)")"</span>
<span class="kd">struct</span> <span class="kt">ServiceDecorator</span><span class="p">:</span> <span class="kt">Service</span> <span class="p">{</span>
    <span class="c1">// generated code will go here</span>
<span class="p">}</span>

<span class="kd">extension</span> <span class="kt">ServiceDecorator</span> <span class="p">{</span>
     <span class="c1">// manually implemented method</span>
    <span class="kd">internal</span> <span class="kd">func</span> <span class="nf">bar</span><span class="p">(</span><span class="n">_</span> <span class="nv">b</span><span class="p">:</span> <span class="kt">String</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">decorated</span><span class="o">.</span><span class="nf">bar</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
    <span class="p">}</span>

<span class="p">}</span>

</code></pre>

<p><strong>Example output:</strong></p>
<pre class="highlight swift"><code><span class="o">...</span>

<span class="c1">// sourcery: decorate = Service</span>
<span class="c1">// sourcery: decorateMethod = print(#function)</span>
<span class="c1">// sourcery: decorateGet = "print("get: \(#function)")"</span>
<span class="c1">// sourcery: decorateSet = "print("set: \(#function)")"</span>
<span class="kd">struct</span> <span class="kt">ServiceDecorator</span><span class="p">:</span> <span class="kt">Service</span> <span class="p">{</span>

<span class="c1">// sourcery:inline:auto:ServiceDecorator.autoDecorated</span>
    <span class="kd">internal</span> <span class="kd">private(set)</span> <span class="k">var</span> <span class="nv">decorated</span><span class="p">:</span> <span class="kt">Service</span>

    <span class="kd">internal</span> <span class="nf">init</span><span class="p">(</span><span class="nv">decorated</span><span class="p">:</span> <span class="kt">Service</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">self</span><span class="o">.</span><span class="n">decorated</span> <span class="o">=</span> <span class="n">decorated</span>
    <span class="p">}</span>

    <span class="kd">internal</span> <span class="k">var</span> <span class="nv">prop1</span><span class="p">:</span> <span class="kt">Int</span> <span class="p">{</span>
        <span class="nf">print</span><span class="p">(</span><span class="s">"get: </span><span class="se">\(</span><span class="kd">#function</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">decorated</span><span class="o">.</span><span class="n">prop1</span>
    <span class="p">}</span>

    <span class="kd">internal</span> <span class="k">var</span> <span class="nv">prop2</span><span class="p">:</span> <span class="kt">Int</span> <span class="p">{</span>
        <span class="k">get</span> <span class="p">{</span>
            <span class="nf">print</span><span class="p">(</span><span class="s">"get: </span><span class="se">\(</span><span class="kd">#function</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">decorated</span><span class="o">.</span><span class="n">prop2</span>
        <span class="p">}</span>
        <span class="k">set</span> <span class="p">{</span>
            <span class="nf">print</span><span class="p">(</span><span class="s">"set: </span><span class="se">\(</span><span class="kd">#function</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
            <span class="n">decorated</span><span class="o">.</span><span class="n">prop2</span> <span class="o">=</span> <span class="n">newValue</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="kd">internal</span> <span class="kd">func</span> <span class="nf">foo</span><span class="p">(</span><span class="nv">f</span><span class="p">:</span> <span class="kt">Int</span><span class="p">,</span> <span class="n">_</span> <span class="nv">a</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="k">throws</span> <span class="o">-&gt;</span> <span class="kt">Int</span> <span class="p">{</span>
        <span class="nf">print</span><span class="p">(</span><span class="kd">#function</span><span class="p">)</span>
        <span class="k">return</span> <span class="k">try</span> <span class="n">decorated</span><span class="o">.</span><span class="nf">foo</span><span class="p">(</span><span class="nv">f</span><span class="p">:</span> <span class="n">f</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
    <span class="p">}</span>
<span class="c1">// sourcery:end</span>
<span class="p">}</span>
<span class="o">...</span>
</code></pre>

          </section>
        </section>
        <section id="footer">
          <p>Copyright © 2016-2021 Pixle. All rights reserved.</p>
          <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="noopener" rel="external">jazzy ♪♫ v0.14.0</a>, a <a class="link" href="https://realm.io" target="_blank" rel="noopener" rel="external">Realm</a> project.</p>
        </section>
      </article>
    </div>
  </body>
</html>
